/**
 * vuex 用户状态管理模块
 */
import { application } from "@/common/config/dictionary.js";
const applicationGetters = {};
application.forEach((item0) => {
  applicationGetters[item0.type] = (state) =>
    state.classificationData.filter((item) => item.type == item0.type);
});

const lifeData = uni.getStorageSync("lifeData") || {};
const baseData = lifeData.baseData || {};

export default {
  namespaced: true,
  state: {
    classificationData: baseData.classificationData || [], //分类数据
    application: application, //应用数据
    rememberLoginForm: baseData.rememberLoginForm || {
      username: "",
      password: "",
    }, //记住登录表单信息
    rememberChecked: baseData.rememberChecked ?? true, //是否记住登录表单信息
  },
  getters: {
    ...applicationGetters,
  },
  mutations: {
    setClassificationData(state, data) {
      uni.vk.setVuex("baseData.classificationData", data);
    },
  },
  actions: {
    async getClassificationData({ commit }) {
      try {
        const res = await uni.vk.callFunction({
          url: "client/classification/kh/getClassificationList",
        });
        commit("setClassificationData", res.data);
        return Promise.resolve();
      } catch (e) {
        return Promise.reject();
      }
    },
  },
};
